package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbvalDataOnDemand.
 */
@Component
@Configurable
@RooDataOnDemand(entity = Ttlsbval.class)
public class TtlsbvalDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbval> data;

	/**
	 * Gets the new transient ttlsbval.
	 *
	 * @param index the index
	 * @return the new transient ttlsbval
	 */
	public Ttlsbval getNewTransientTtlsbval(int index) {
        Ttlsbval obj = new Ttlsbval();
        setDesUrlval(obj, index);
        setDesVali(obj, index);
        if (String.valueOf(index).length() > 4){
        	obj.setCodVali(String.valueOf(index).substring(0,5));
        }else{
        	obj.setCodVali(String.valueOf(index));
        }
        return obj;
    }

	/**
	 * Sets the des urlval.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesUrlval(Ttlsbval obj, int index) {
        String desUrlval = "desUrlval_" + index;
        if (desUrlval.length() > 80) {
            desUrlval = desUrlval.substring(0, 80);
        }
        obj.setDesUrlval(desUrlval);
    }

	/**
	 * Sets the des vali.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesVali(Ttlsbval obj, int index) {
        String desVali = "desVali_" + index;
        if (desVali.length() > 20) {
            desVali = desVali.substring(0, 20);
        }
        obj.setDesVali(desVali);
    }

	/**
	 * Gets the specific ttlsbval.
	 *
	 * @param index the index
	 * @return the specific ttlsbval
	 */
	public Ttlsbval getSpecificTtlsbval(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbval obj = data.get(index);
        return Ttlsbval.findTtlsbval(obj.getCodVali());
    }

	/**
	 * Gets the random ttlsbval.
	 *
	 * @return the random ttlsbval
	 */
	public Ttlsbval getRandomTtlsbval() {
        init();
        Ttlsbval obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbval.findTtlsbval(obj.getCodVali());
    }

	/**
	 * Modify ttlsbval.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbval(Ttlsbval obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbval.findTtlsbvalEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbval' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbval>();
        for (int i = 0; i < 10; i++) {
            Ttlsbval obj = getNewTransientTtlsbval(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
